package st.business.tepth.manager;


import java.util.List;

import st.business.tepth.business.bean.CommonBean;
import st.shadow.library.core.storage.ShadowPreference;

/**
 * @author zhangyu
 * @date 2019/1/14
 */
public class UserManager {
    private enum SignTag {
        SIGN_TAG
    }

    private UserBean mUserBean;

    private UserManager() {
    }

    private static final class Holder {
        private static final UserManager INSTANCE = new UserManager();
    }

    public static UserManager getInstance() {
        return Holder.INSTANCE;
    }

    public UserBean getCurrentUser() {
        return mUserBean;
    }

    public String getCurrentUserCode() {
        if (mUserBean != null) {
            if (mUserBean.getBaseInfo() != null) {
                return mUserBean.getBaseInfo().getJobNo();
            }
        }
        return "";
    }

    public String getCurrentUserName() {
        if (mUserBean != null) {
            if (mUserBean.getBaseInfo() != null) {
                return mUserBean.getBaseInfo().getUserName();
            }
        }
        return "";
    }

    public String getToken() {
        if (mUserBean != null && mUserBean.getBaseInfo() != null) {
            return mUserBean.getBaseInfo().getToken();
        } else {
            return "";
        }
    }

    public void checkUser(IUserChecker checker) {
        if (isSignIn()) {
            checker.onSignIn(getCurrentUser());
        } else {
            checker.onNotSignIn();
        }
    }

    public boolean isSignIn() {
        return ShadowPreference.getAppFlag(SignTag.SIGN_TAG.name());
    }

    private void setSignState(boolean state) {
        ShadowPreference.setAppFlag(SignTag.SIGN_TAG.name(), state);
    }


    /**
     * 清除用户信息
     */
    public void clearUserInfo() {
        setSignState(false);
    }

    public void saveUser(UserBean userBean) {
        mUserBean = userBean;
        setSignState(true);
    }

    public boolean isGroupLeader() {
        if (mUserBean != null) {
            UserBean.BaseInfoBean baseInfoBean = mUserBean.getBaseInfo();
            if (baseInfoBean != null) {
                List<CommonBean> roles = baseInfoBean.getRoles();
                if (roles != null) {
                    for (CommonBean commonBean : roles) {
                        //班组长
                        if (commonBean.getId() == 17) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
}
